var RunLayer = cc.Layer.extend({
	arr:null,
	scene:null,
	clock:null,
	beaLine:null,
	ctor:function (parent) {
		this._super();
		this.scene = parent;
		this.scene.addChild(this, 10);
		gg.main = this;
		this.init();
	},
	init:function () {
		this.callNext = cc.callFunc(function(){
			gg.flow.next();
		}, this);
		this.callKill = cc.callFunc(function(p){
			var seq = cc.sequence(cc.fadeOut(0.5),cc.callFunc(function(){
				p.removeFromParent(true);	
			}, this));
			p.runAction(seq);
		}, this); 
		
		this.callNext.retain();
		this.callKill.retain();
		this.clock = new Clock(this);
		var close = new Button(this, 90, TAG_CLOSE, "#button/close.png",this.callback);
		close.setPosition(cc.p(980,620));
		close.setVisible(false);
		

		

//		var pen = new Pen(this);
//		pen.setPosition(710,490);

		// 物品库
		this.lib = new Lib(this);
		
		this.scheduleOnce(function(){

		},1);
	},
	loadTest:function(){
 this.lib.moveLib(TAG_LIB_SALT,400);

	},
	test2:function(){
		var move = cc.moveTo(0.7,cc.p(570,320));
		var rotate = cc.rotateTo(0.8, 90);
		var func = cc.callFunc(function(){
			ss.pour2();
			ss.hide(3, true);
		},this);
		var seq = cc.sequence(move, rotate, func);
		ss.runAction(seq);
	},
	loadSalt:function(pos){
		this.salt = new SampleBottle(this);
		this.addChild(this.salt, 20);
		this.loadInLib(this.salt, pos, cc.p(600, 250));
	},
	loadCyl:function(pos){
		var cylinder=new Cylinder(this);
		this.loadInLib(cylinder, pos, cc.p(430,150));
		
		
	},
	loadCyl1:function(pos){
		var cylinder1=new Cylinder1(this);
		this.loadInLib(cylinder1, pos, cc.p(680,150));
	},
	loadKettle:function(pos){
		  var kettle=new Kettle(this);

		this.loadInLib(kettle, pos, cc.p(800,430));
	},
	loadVolumetric:function(pos)
	{
		var volumetric =new Volumetric(this);
	
		this.loadInLib(volumetric, pos, cc.p(420,150));
		
	
	},



//	loadBurette:function(pos){
//		var burette =new Button(this,20,TAG_BURETTE,"#burette.png",this.callback);
//		this.loadInLib(burette, pos, cc.p(420,260));
//		burette.runAction(cc.ScaleTo(0, 0.7, 0.7));
//		
//	},
	loadIron:function(pos){
		this.ironNode = new cc.Node();
		this.ironNode.setCascadeOpacityEnabled(true);
		this.addChild(this.ironNode,8);
		
		var iron = new Button(this.ironNode, 10, TAG_IRON, "#ironShelf.png");
		this.loadInLib(iron, pos, cc.p(200, 300));
	},
	loadBeaker:function(pos){
       
		var beaker =new Beaker(this);			
	    this.loadInLib(beaker, pos, cc.p(550,120));
			
			

	},
	loadGesuan:function(pos){
		var gesuan = new Button(this, 11, TAG_GESUAN, "#gesuan1.png",this.callback);
		var label = new cc.LabelTTF("铬酸",gg.fontName,15);
		label.setColor(cc.color(0,0,0));
		label.setPosition(gesuan.width*0.5,gesuan.height*0.43);
		gesuan.addChild(label, 11);
		this.loadInLib(gesuan, pos, cc.p(550, 120));
	},
	loadYiye:function(pos){
		var yiyeguan = new Button(this, 9, TAG_YIYE,"#yiye3.png", this.callback);
		yiyeguan.setCascadeOpacityEnabled(true);
		this.loadInLib(yiyeguan, pos, cc.p(700,200));
	},
	loadXierqiu:function(pos){
		var xierqiu = new Xierqiu(this);
		this.loadInLib(xierqiu, pos, cc.p(450,200));

	},
	loadWater:function(pos){
		var water = new Button(this, 10, TAG_WATER,"#water.png", this.callback);
		this.loadInLib(water, pos, cc.p(880,430));
	},
	loadXishuizhi:function(pos){
		//		var xishuizhi = new Button(this, 8, TAG_XISHUIZHI,"#xishuizhi.png", this.callback);
		var xishuizhi = new Button(this, 8, TAG_XISHUIZHI,"#xishuizhi.png", this.callback);
//		this.loadInLib(xishuizhi, pos, cc.p(800,200));
		xishuizhi.setPosition(pos);
		var xishuizhi1 =new cc.Sprite("#xishuizhi1.png");
		xishuizhi1.setPosition(xishuizhi.width*0.5,xishuizhi.height*0.5);
		xishuizhi.addChild(xishuizhi1);
		xishuizhi.setOpacity(0);
		var move = cc.moveTo(1,cc.p(700,80));
		var moveback = cc.moveTo(1,cc.p(1000,400));
		var fadein = cc.fadeIn(1);
		fadein.retain();
		var spawn = cc.spawn(moveback,cc.callFunc(function(){xishuizhi.runAction(fadein)},this));
		var delay= cc.delayTime(1);;
		var seq = cc.sequence(move,delay,spawn,cc.callFunc(this.flowNext,this),cc.callFunc(function(){
			xishuizhi1.removeFromParent();
		},this));
		xishuizhi.runAction(seq);
	},
	loadSbeaker:function(pos){
		var sbeaker = new Button(this, 10, TAG_SBEAKER,"#sbeaker.png", this.callback);
		this.loadInLib(sbeaker, pos, cc.p(550,120));
	},
	loadBeakerflask:function(pos){
		var beakerflask = new Button(this, 10, TAG_BEAKERFLASK,"#16.png", this.callback);
		this.loadInLib(beakerflask, pos, cc.p(600,120));
	},
	loadJianshinode:function(pos){
		var jianshinode = new Jianshi(this);
		this.loadInLib(jianshinode,pos,cc.p(450,250));
	},
	loadIron:function(pos){
		var iron = new Button(this, 9, TAG_IRON,"#31.png", this.callback);
		this.loadInLib(iron, pos, cc.p(170,280));
	},


	loadRod:function(pos){
		
		var tag = null;
		var action = gg.flow.flow.action;
		if(action == ACTION_DO1){
			tag = TAG_ROD_NODE;
		} else if(action == ACTION_DO2){
			tag = TAG_ROD_NODE2;
		}
		
		var rod=new Rod(this,tag);
	
		this.loadInLib(rod, pos, cc.p(400, 400), 1);
	},

	loadDrop:function(pos){
		var tag = null;
		var action = gg.flow.flow.action;
		if(action == ACTION_DO1){
			tag = TAG_HHCL_NODE;
		} else if(action == ACTION_DO2){
			tag = TAG_LIB_BURETTE;
		} else if(action == ACTION_DO3){
			tag = TAG_HBOTTLE_NODE;
		} else if(action == ACTION_DO4){
			tag = TAG_NBOTTLE_NODE;
		} else if(action == ACTION_NONE){
			tag = TAG_ALCOHOL;
		}
		var drop = new DropBottle(this, tag);
		if(action == ACTION_DO2){
			this.loadInLib(drop, pos, cc.p(600,320), 1);
		}else {
			this.loadInLib(drop, pos, cc.p(600,350), 1);
		}
		
		
	},

	loadInLib:function(obj, pos, tarPos,delay){
		obj.setPosition(pos);
		if(delay == null){
			delay = 1;
		}
		var ber = $.bezier(pos, tarPos, delay);
		var seq = cc.sequence(ber, this.callNext);
		obj.runAction(seq);
	},
	kill:function(obj){
		var fade = cc.fadeTo(0.5,0);
		var func = cc.callfunc(function(){
			obj.removeFromParent(true);
		},this);
		var seq = cc.sequence(fade,func);
		obj.runAction(seq)
	},
	callback:function (p){
		var action = gg.flow.flow.action;
		var func = cc.callFunc(this.actionDone, this);
		
		switch(p.getTag()){

		 
		    	
		    
			
		
			
//			case TAG_BEAKER:
//				var ber = cc.moveTo(1,cc.p(550,520));
//				var rotate = cc.rotateTo(1, -80);
//				var spawn = cc.spawn(ber,rotate);
//				var delay = cc.delayTime(1.5);
//				var moveBack = cc.moveTo(1,cc.p(650,430));
//				var rotate = cc.rotateTo(1, 0);
//				var spaw2 = cc.spawn(moveBack,rotate);
//				var fade = cc.fadeTo(1,0);			
//				
//				var hbottlenode = this.getChildByTag(TAG_HBOTTLE_NODE);
//				var hcork1 = hbottlenode.getChildByTag(TAG_HCORK1);
//				var moveBack1 = cc.moveTo(1,cc.p(0,65));
//				var rotate1 =cc.rotateTo(1,0);
//				var spawn1 = cc.spawn(moveBack1,rotate1);
//				
//				var seq1 = cc.sequence(cc.delayTime(3),spawn1,cc.callFunc(function(){
//					hbottlenode.runAction(cc.moveBy(0.5,cc.p(-50,0)));
//				},this));
//				var seq = cc.sequence(spawn,func,delay,spaw2,fade,cc.callFunc(this.flowNext,this));
//				
//				hcork1.runAction(seq1);
//				p.runAction(seq);
//				break;

			
				
			case TAG_CLOSE:
				if (action==ACTION_DO1){
					var win = this.getChildByTag(TAG_WINDOW);
					win.removeFromParent();
					p.setVisible(false);
					var show = new ShowTip(this,"\n\n同样方法，用该清洗液再重复清洗移液管2次\n\n" ,cc.p(700,300));
					this.flowNext();
				}else if (action==ACTION_DO2){
					var win = this.getChildByTag(TAG_WINDOW);
					win.removeFromParent();
					p.setVisible(false);
					this.flowNext();
				}else if (action==ACTION_DO3){
					var win = this.getChildByTag(TAG_WINDOW);
					win.removeFromParent();
					p.setVisible(false);
					var show = new ShowTip(this,"\n\n同样方法，用该清洗液再重复清洗滴定管2次\n\n" ,cc.p(700,300));
					this.flowNext();
				}else if (action==ACTION_DO4){
					this.getChildByTag(TAG_BAOGAO).setVisible(false);
					p.setVisible(false);
					this.flowNext();
				}else if (action==ACTION_DO5){
					this.getChildByTag(TAG_BAOGAO).setVisible(false);
					p.setVisible(false);
					var show = new ShowTip(this,"\n\n平行滴定三次，求出平均值和相对平均偏差值\n\n" ,cc.p(700,300));
					this.flowNext();
				}else if (action==ACTION_DO6){
					var win = this.getChildByTag(TAG_WINDOW);
					win.removeFromParent();
					p.setVisible(false);
					this.flowNext();
				}
				
				break;
			case TAG_WATER:
				if(action==ACTION_DO1){
					var move = cc.moveTo(1,cc.p(910,230));
					var rotate= cc.rotateTo(0.5,-60);
					var moveback=cc.moveTo(1,cc.p(880,430));
					var rotate1= cc.rotateTo(1,0);
					var spawn1 = cc.spawn(moveback,rotate1);
					var delay = cc.delayTime(1.5);
					var delay1 = cc.delayTime(0.5);

//					var yiyeguan= this.getChildByTag(TAG_YIYE);
//					var rotate2 = cc.rotateTo(1.5,45);
//					var rotate4 = cc.rotateTo(1,90);
//					var rotate3=cc.rotateTo(1,0);

					//var seq1= cc.sequence(rotate2,delay,rotate4,delay,rotate3,cc.callFunc(this.flowNext,this));
					//yiyeguan.runAction(seq1);

					var seq = cc.sequence(move,rotate,func,delay,spawn1,cc.callFunc(this.flowNext,this));
					p.runAction(seq);			
				}
				if(action==ACTION_DO2){
					
					var iron = this.getChildByTag(TAG_IRON);
					var move = cc.moveTo(1.5,cc.p(470,280));
					//var move2 = cc.moveTo(1.5,cc.p(200,170));
					var seq1 = cc.sequence(cc.delayTime(4),move,cc.delayTime(5));
					iron.runAction(seq1);
					
				
					
					var jianshinode = this.getChildByTag(TAG_JIANSHI_NODE);
					var move = cc.moveTo(1.5,cc.p(450,250));
					var delay = cc.delayTime(1);
					var delay1 = cc.delayTime(1.5);
					var rotate = cc.rotateTo(1,45);
					var move5 = cc.moveTo(1,cc.p(450,380));
					
					var rotate1=cc.rotateTo(1,0);
					var spawn11= cc.spawn(move5,rotate1);
					
					var move1 = cc.moveTo(1.5,cc.p(636,325));
					var rotate2= cc.rotateTo(1.5,-60);
					var moveback=cc.moveTo(1.5,cc.p(880,430));
					var rotate3= cc.rotateTo(1.5,0);
					var spawn1 = cc.spawn(moveback,rotate3);
					var delay2 = cc.delayTime(4);
					var delay3 = cc.delayTime(0.5);
					var seq = cc.sequence(move,rotate,delay3,func,delay1,spawn11);
					jianshinode.runAction(seq);;
					var seq1 = cc.sequence(move1,rotate2,func,delay1,spawn1,cc.callFunc(function(){
						this.clock.doing();},this),delay2,cc.callFunc(function(){
							this.clock.stop();},this),
							cc.callFunc(this.flowNext,this));
					p.runAction(seq1);
					
					var lanse = new cc.Sprite("#33.png");
					lanse.setPosition(0,-15);
					lanse.setOpacity(0);
					jianshinode.addChild(lanse,12,TAG_SHOW);
					var fadein = cc.fadeIn(0.5);
					var sequ = cc.sequence(cc.delayTime(5.5),fadein);
					lanse.runAction(sequ);
					
				}
				if(action==ACTION_DO3){
					var move = cc.moveTo(1,cc.p(636,325));
					var rotate= cc.rotateTo(0.5,-60);
					var moveback=cc.moveTo(1,cc.p(880,430));
					var rotate1= cc.rotateTo(1,0);
					var spawn1 = cc.spawn(moveback,rotate1);
					var delay = cc.delayTime(1);
					var delay1 = cc.delayTime(0.5);

					var jianshi= this.getChildByTag(TAG_JIANSHI_NODE);
					var rotate2 = cc.rotateTo(1.5,45);
					var rotate3=cc.rotateTo(1,0);

					var seq1= cc.sequence(rotate2,delay,rotate3);
					jianshi.runAction(seq1);

					var seq = cc.sequence(move,rotate,func,delay,spawn1,cc.callFunc(this.flowNext,this));
					p.runAction(seq);
				}
				break;
		
		
		
				

		}
	},
	beakerToNet:/**
				 * 把烧杯挪到石棉网
				 */
	function(p){
		var move = cc.moveTo(0.6, cc.p(645, 290));
		var seq = cc.sequence(move, this.callNext);
		p.runAction(seq);
	},
	beakerYinliu:/**
					 * 烧杯引流
					 */
	function(p, tag){
		// 通过引流棒过滤
		var bezier = cc.bezierTo(1, [cc.p(520,340),cc.p(385,540),cc.p(228,375)]);
		var rotate = cc.rotateTo(0.8, -90);
		var seq = cc.sequence(bezier,cc.callFunc(function(){
			// 烧杯水位线下降
			p.pour();
			p.hide(3);
			// 烧杯水位线上升
			var beaker = this.getChildByTag(tag);
			beaker.upLine(true);
		}, this),rotate);
		p.runAction(seq);
	},
	actionDone:function(p){
		var action = gg.flow.flow.action;
		var func = cc.callFunc(this.actionDone, this);
		switch(p.getTag()){
//			case TAG_PAPER:
//				p.runAction(cc.fadeTo(0.5, 0));
//				var keaber = this.getChildByTag(TAG_BEAKER);
//				keaber.showSalt(true);
//				break;
			
			
		
		
		}
	},
	flowNext:function(){
		gg.flow.next();
	},
	onExit:function(){
		this._super();
		this.callNext.release();
		this.callKill.release();
	}
});